package com.hnucm.springjdbc.controller;

import com.hnucm.springjdbc.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class BookController {
    @Autowired
    BookService bookService;
    @RequestMapping("bookslist")
    public String showbooks(Model model){
        model.addAttribute("BooksList",bookService.findAllBooks());
        return "bookslist";
    }
    @RequestMapping("addbooks")
    public String addbooks(){
        return "addbooks";
    }
    @RequestMapping("addbookscommit")
    public String addbookscommit(Model model, String name,float price,int amount) {
        bookService.addBooks(name, price, amount);
        return "forward:/bookslist";
    }
    @RequestMapping("deletebooks")
    public String deletebooks(int id){
        bookService.deleteBooks(id);
        return "forward:/bookslist";
    }
    @RequestMapping("updatabooks")
    public String updatabooks(Model model,int id,String name,double price,int amount){
        model.addAttribute("id",id);
        model.addAttribute("name",name);
        model.addAttribute("price",price);
        model.addAttribute("amount",amount);
        return "updatabooks";
    }
    @RequestMapping("updatabookscommit")
    public String updatabookscommit(int id,String name,double price,int amount) {
        bookService.updataBooks(id, name, price, amount);
        return "forward:/bookslist";
    }
}
